package com.liwbn.rbac.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * 异常处理
 * 
 * @author liwb
 */
public class IresExceptionHandler implements HandlerExceptionResolver {

    private static final Logger log = LoggerFactory
            .getLogger(IresExceptionHandler.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object obj, Exception exception) {
        log.error(exception.getMessage());
        exception.printStackTrace();
        if (exception instanceof AuthorizationException) {
            return new ModelAndView("redirect:/error/auth");
        } else if (exception instanceof JsonReException) {
            JsonReException jsonReException = (JsonReException) exception;
            ModelAndView mav = new ModelAndView("jsonView");
            mav.addObject("code", jsonReException.getCode());
            mav.addObject("message", jsonReException.getMessage());
            return mav;
        }
        return new ModelAndView("redirect:/error");
    }

}
